//
//  VistorLoginView.swift
//  ydy_weibo
//
//  Created by 杨德勇 on 16/7/12.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

import UIKit

@objc protocol VistorLoginViewDelegate:NSObjectProtocol {
    //协议方法
    optional func vistorWillLogin()
    optional func vistorWillRegister()
}


class VistorLoginView: UIView {
    //代理属性
    weak var vistorDelegate:VistorLoginViewDelegate?
    
    @objc func loginBtnDidClick(){
        vistorDelegate?.vistorWillLogin?()
        
    }
    @objc func registerBtnDidClick(){
        vistorDelegate?.vistorWillRegister?()
        
    }
    
    

    

    //设置页面信息方法（给别的视图改变用的。）
    func setUIInfo(imageName:String?,title:String) {
        
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            tipLabel.text = title
            iconView.hidden = true
            backView.hidden = true
        }
        
        if imageName == nil {
            startAnimation()
        }
        
    }
    //让circle view动起来
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 20                  //duration越小速度越快
        anim.removedOnCompletion = false    //动画不活跃的时候，动画不移除，在后台进行，除非访客视图消失，消耗很小
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    //重写构造方法
    init(){
        super.init(frame: CGRectZero)
        setupUI()
        
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("coder false")
    }
    
    
    //设置访客试图
    private func setupUI(){
//        backgroundColor = UIColor.orangeColor()
        addSubview(circleView)
        
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        
        //设置布局,VFL
        //消除autoresizeing，不然手动添加的约束没效果
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
//        circleView.translatesAutoresizingMaskIntoConstraints = false;
////        tipLabel.translatesAutoresizingMaskIntoConstraints = false;
//        iconView.translatesAutoresizingMaskIntoConstraints = false;
        
        //添加圆圈约束
        addConstraint(
        NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(
        NSLayoutConstraint(item: circleView, attribute: .CenterY,relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
//        //中心视图的约束
        addConstraint(
            NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(
            NSLayoutConstraint(item: iconView, attribute: .CenterY,relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
    
    //为文字控件添加约束
        addConstraint(
            NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(
            NSLayoutConstraint(item: tipLabel, attribute: .CenterY,relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(
            NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224)
        )
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))

    //为登陆按钮加入约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
      //为注册按钮添加约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //为backview添加约束
        addConstraint(NSLayoutConstraint(item: backView, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backView, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: backView, attribute: .Bottom, relatedBy: .Equal, toItem: loginBtn, attribute: .Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 450))
        
     //更换背景颜色
        backgroundColor = UIColor.init(white: 0.93, alpha: 1)
     //添加点击事件
        loginBtn.addTarget(self, action: #selector(VistorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(VistorLoginView.registerBtnDidClick), forControlEvents: .TouchUpInside)
     
    }
    
    
    
    


    //懒加载所有控件
    private lazy var circleView :UIImageView = UIImageView(image : UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView :UIImageView = UIImageView(image : UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView :UIImageView = UIImageView(image : UIImage(named: "visitordiscover_feed_mask_smallicon"))

    
    

    private lazy var tipLabel : UILabel = {
        let l = UILabel()
        l.text = "关注一些人，回这里看看什么惊喜！"
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(14)
        l.numberOfLines = 0
        l.textColor = UIColor.grayColor()
        l.sizeToFit()
        return l
    }()
    
    
    private lazy var loginBtn: UIButton = {
       let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
        
        return btn
    }()
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        
        
        return btn
    }()
}
